﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using InvoiceProject.Enums;

namespace InvoiceProject.Helpers
{
    public static class ExtensionMethods
    {

        public static SelectList ToSelectList<TEnum>(this TEnum enumObj)
        {
            var values = from TEnum e in Enum.GetValues(typeof(TEnum))
                         select e;
          
            return new SelectList(values, "Id", "Name", enumObj);
        }

        public static bool IsNullOrEmpty(this String s)
        {
            return String.IsNullOrEmpty(s);
        }

       
    }


    public static class SelectExtensions
    {

        public static string GetInputName<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)
        {
            if (expression.Body.NodeType == ExpressionType.Call)
            {
                MethodCallExpression methodCallExpression = (MethodCallExpression)expression.Body;
                string name = GetInputName(methodCallExpression);
                return name.Substring(expression.Parameters[0].Name.Length + 1);

            }
            return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1);
        }

        private static string GetInputName(MethodCallExpression expression)
        {
            // p => p.Foo.Bar().Baz.ToString() => p.Foo OR throw...
            MethodCallExpression methodCallExpression = expression.Object as MethodCallExpression;
            if (methodCallExpression != null)
            {
                return GetInputName(methodCallExpression);
            }
            return expression.Object.ToString();
        }

  

        //public static MvcHtmlString EnumDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) where TModel : class
        //{
        //    string inputName = GetInputName(expression);
        //    var value = htmlHelper.ViewData.Model == null
        //        ? default(TProperty)
        //        : expression.Compile()(htmlHelper.ViewData.Model);

        //    return htmlHelper.DropDownList(inputName, ToSelectList(typeof(TProperty), value.ToString()));
        //}


    
            /// <summary>
            /// Retrieve the description on the enum, e.g.
            /// [Description("Bright Pink")]
            /// BrightPink = 2,
            /// Then when you pass in the enum, it will retrieve the description
            /// </summary>
            /// <param name="en">The Enumeration</param>
            /// <returns>A string representing the friendly name</returns>
            public static string GetEnumDescription(Enum en)
            {
                Type type = en.GetType();

                MemberInfo[] memInfo = type.GetMember(en.ToString());

                if (memInfo != null && memInfo.Length > 0)
                {
                    object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false);

                    if (attrs != null && attrs.Length > 0)
                    {
                        return ((DisplayAttribute)attrs[0]).Description;
                    }
                }

                return en.ToString();
            }

            private static T StringToEnum<T>(string name)
            {
                string[] names = Enum.GetNames(typeof(T));
                if (((IList)names).Contains(name))
                {
                    return (T)Enum.Parse(typeof(T), name);
                }
                else return default(T);
            }



            public static MvcHtmlString ToAccountSelectList<T>(this T enumeration, string selected)
            {

                const string selectedHtml = "selected='selected'";
                const string optionHtml = "<option value='{0}' {1} >{2}</option>";
                var source = Enum.GetValues(typeof(AccountType));
                var result = "<select name='AccountType' id='AccountType' data-native-menu='false' data-inline='true'>" + Environment.NewLine;
                var items = new Dictionary<object, string>();

                var displayAttributeType = typeof(DisplayAttribute);

                foreach (var value in source)
                {
                    string enumDescription = GetEnumDescription(StringToEnum<AccountType>(value.ToString()));
                    if (selected == value.ToString())
                        result += String.Format(optionHtml, value, selectedHtml,enumDescription );
                    else
                    {
                        result += String.Format(optionHtml, value, null, enumDescription);

                    }
                    result += Environment.NewLine;


                }
                result += "</select>";

                return new MvcHtmlString(result);
            }

        public static MvcHtmlString ToCountryModelSelectList<T>(this T enumeration, string selected)
        {
            const string selectedHtml = "selected='selected'";
            const string optionHtml = "<option value='{0}' {1} >{2}</option>";
            var source = Enum.GetValues(typeof(Country));
            var result = "<select name='LookupTaxCountry.Country' id='custom' data-native-menu='false' data-inline='true'>" +Environment.NewLine;
            var items = new Dictionary<object, string>();

            var displayAttributeType = typeof(DisplayAttribute);

            foreach (var value in source)
            {
              
                if (selected == value.ToString())
                    result +=  String.Format(optionHtml, value, selectedHtml, GetEnumDescription(StringToEnum<Country>(value.ToString())) );
                else
                {
                    result += String.Format(optionHtml, value, null,value);

                }
                result += Environment.NewLine;


            }
            result += "</select>";

            return new MvcHtmlString(result);
        }




    }



}








public class StringValueAttribute : System.Attribute
{

    private string _value;

    public StringValueAttribute(string value)
    {
        _value = value;
    }

    public string Value
    {
    get { return _value; }
    }
   
}



